D:\a\tools.proto\tools.proto\compiler\src\gen\rust\structure\mod.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::structure::Structure; |
30 | | use crate::compiler::util::types::{Name, TypePathMap}; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::structure::{generate, Templates}; |
33 | | use crate::gen::rust::util::RustUtils; |
34 | | use crate::gen::template::hook::{Fragment, TemplateHooks}; |
35 | | use crate::gen::template::{Options, Template}; |
36 | | use crate::gen::RustParams; |
37 | | |
38 | | const STRUCT_TEMPLATE: &[u8] = include_bytes!("core.template"); |
39 | | const STRUCT_FIELD_TEMPLATE: &[u8] = include_bytes!("field.template"); |
40 | | const STRUCT_BITS_TEMPLATE: &[u8] = include_bytes!("bin.template"); |
41 | | const STRUCT_RAW_TEMPLATE: &[u8] = include_bytes!("raw.template"); |
42 | | |
43 | 59 | pub fn gen_structure_decl(s: &Structure, type_path_map: &TypePathMap, params: &RustParams) -> String { |
44 | 59 | let mut options = Options::default(); |
45 | 59 | if params.disable_read.contains(s.name()) { Branch (45:8): [True: 0, False: 59]
Branch (45:8): [Folded - Ignored]
|
46 | 0 | options.disable("from_bytes").disable("getters"); |
47 | 59 | } |
48 | 59 | if params.disable_write.contains(s.name()) { Branch (48:8): [True: 0, False: 59]
Branch (48:8): [Folded - Ignored]
|
49 | 0 | options.disable("write_to").disable("write_to_async").disable("setters"); |
50 | 59 | } |
51 | 59 | let templates = Templates { |
52 | 59 | template: Template::compile_with_options(STRUCT_TEMPLATE, &options).unwrap(), |
53 | 59 | field_template: Template::compile_with_options(STRUCT_FIELD_TEMPLATE, &options).unwrap(), |
54 | 59 | bits_template: Template::compile_with_options(STRUCT_BITS_TEMPLATE, &options).unwrap(), |
55 | 59 | raw_template: Template::compile_with_options(STRUCT_RAW_TEMPLATE, &options).unwrap(), |
56 | 59 | }; |
57 | 59 | let mut hooks = TemplateHooks::new(); |
58 | 59 | if params.enable_write_async { Branch (58:8): [True: 11, False: 48]
Branch (58:8): [Folded - Ignored]
|
59 | 11 | hooks.hook("ext", Fragment::new("", &["write_to_async"])); |
60 | 48 | } |
61 | 59 | if s.is_used_in_header() || params.enable_struct_to_mut54 { Branch (61:8): [True: 5, False: 54]
Branch (61:33): [True: 0, False: 54]
Branch (61:8): [Folded - Ignored]
Branch (61:33): [Folded - Ignored]
|
62 | 5 | hooks.hook("ext", Fragment::new("", &["to_mut"])); |
63 | 54 | } |
64 | 59 | if s.is_used_in_header() || params.enable_struct_dupe.contains(s.name())54 { Branch (64:8): [True: 5, False: 54]
Branch (64:33): [True: 1, False: 53]
Branch (64:8): [Folded - Ignored]
Branch (64:33): [Folded - Ignored]
|
65 | 6 | hooks.hook("ext", Fragment::new("", &["dupe"])); |
66 | 53 | } |
67 | 59 | generate::<RustUtils, _>( |
68 | 59 | templates, |
69 | 59 | s, |
70 | 59 | &TypePathMapper::new(type_path_map, DefaultTypeMapper), |
71 | 59 | &hooks, |
72 | 59 | ) |
73 | 59 | } |
74 | | |
75 | | #[cfg(test)] |
76 | | mod tests { |
77 | | use crate::gen::rust::structure::STRUCT_TEMPLATE; |
78 | | use crate::gen::template::Template; |
79 | | |
80 | | #[test] |
81 | 1 | fn test_template_render() { |
82 | 1 | let mut template = Template::compile(STRUCT_TEMPLATE).unwrap(); |
83 | 1 | template.var("name", "Test").var("struct_description", ""); |
84 | 1 | let code = template.render("", &["decl"]).unwrap(); |
85 | 1 | assert_eq!( |
86 | 1 | &*code, |
87 | 1 | "/// Definition for the Test structure. |
88 | 1 | /// |
89 | 1 | /// |
90 | 1 | #[derive(Copy, Clone, Default, Debug)] |
91 | 1 | pub struct Test<T> { |
92 | 1 | raw: RawTest<T> |
93 | 1 | } |
94 | 1 | " |
95 | 1 | ) |
96 | 1 | } |
97 | | } |